﻿using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Input;
using TestFireBroadcastingHostPhone.RecordData;
using TestFireBroadcastingHostPhone.Source;
using TestFireBroadcastingHostPhone.ViewModel;

namespace TestFireBroadcastingHostPhone
{
    public partial class MainWindow : Window
    {
        /// <summary>
        /// 配置文件保存路径
        /// </summary>
        public const string configpath = "C:\\盈帜软件配置\\RecordData\\RelatedRecord.ini";
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new MainWindowMV();
        }

        private void Window_Closed(object sender, EventArgs e)
        {
            Application.Current.Shutdown();
        }
        /// <summary>
        /// 关闭窗口之前的事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            string err_info = string.Empty;
            string default_file_path = Directory.GetCurrentDirectory() + "\\RecordData\\RelatedRecord.ini";
            //关闭软件前，将测试信息记录到日志中
            LogsQueryMV.logsQueryMV.DataGrid_Process_Add_Fun(AutoTestConfig.Default.测试产品型号, SystemConfigMV.systemconfigMV.SuccessTestNumber.ToString() + "台合格",
                SystemConfigMV.systemconfigMV.TestNumber.ToString(), AutoTestConfig.Default.测试人员, AutoTestConfig.Default.测试日期,out err_info);
            if (string.IsNullOrEmpty(err_info)) SaveDataGridToTextFile(LogsQueryMV.logsdata);//将表格控件内容写入Txt文本
            if (!StaticInfo.instrumentsControl.open_power)
            {
                return;
            } 
            else
            {
                MessageBoxResult result = MessageBox.Show("当前测试下未关闭工装继电器和程控交流源，退出前是否关闭？", "确认", MessageBoxButton.OK);
                if (result == MessageBoxResult.OK)
                {
                    StaticInfo.frockControl.Stop_ShortOutputTest(out err_info);//控制工装关短路
                    Thread.Sleep(800);
                    StaticInfo.frockControl.Close_All(out err_info);
                    StaticInfo.instrumentsControl.CloseACPower(SystemConfigMV.systemconfigMV.Cmb_ACPowerSelect_SelectValue,SystemConfigMV.systemconfigMV.Cmb_AN97002SerialPort_SelectValue!,out err_info);
                    if (!string.IsNullOrEmpty(err_info))
                    {
                        MessageBox.Show("关闭程控交流源失败!");
                    }
                }
                else if(result == MessageBoxResult.Cancel)
                {
                    e.Cancel = true;
                }
            }
        }
        /// <summary>
        /// 将DataGrid控件数据记录到Txt文本中
        /// </summary>
        /// <param name="dataGrid">DataGrid控件</param>
        /// <param name="filePath">Txt文本</param>
        public void SaveDataGridToTextFile(string filePath)
        {
            var sb = new StringBuilder();
            sb.Append("测试产品\t测试结果\t测试数量\t测试人员\t测试时间\n");
            foreach (var item in LogsQueryMV.logsQueryMV.LogEntries)
            {
                sb.Append(item.TestProduct + "\t");
                sb.Append(item.TestResult + "\t");
                sb.Append(item.TestNumber + "\t");
                sb.Append(item.TestPerson + "\t");
                sb.Append(item.TestTime + "\n");
            }
            File.WriteAllText(filePath, sb.ToString());
        }
        /// <summary>
        /// 窗体最小化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_MinimizeButtonClick(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }
        /// <summary>
        /// 窗体最大化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_MaximizeOrRestoreButtonClick(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == WindowState.Normal)
                this.WindowState = WindowState.Maximized;
            else
                this.WindowState = WindowState.Normal;
        }
        /// <summary>
        /// 关闭窗体
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_CloseButtonClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// 窗口挪动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void TitleBar_MouseDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            // 检查是否是左键点击，以避免其他鼠标按键导致意外行为
            if (e.ChangedButton == MouseButton.Left)
            {
                // 如果窗口是最大化的，则先还原窗口大小以便正常拖动
                if (this.WindowState == WindowState.Maximized)
                {
                    this.WindowState = WindowState.Normal;
                }
                this.DragMove();
            }
        }
    }
}
